var server = require('../../../utils/server');
var search;
Page({
	data: {
		orderId: ''
	},
	onLoad: function (options) {
		
		var app = getApp();
		var order = app.globalData.order;
		var orderId = order.order_id;
    console.log(order)
		this.setData({order:order});
		console.log('order id : ' + orderId);
    var info = wx.getStorageSync("sysinfo");
    console.log(info)

    server.getJSON('/Cart/getWXPayData/user_id/' + 1 +"/order_id/" + orderId+"/openid/"+info.openid,function(res){
app.globalData.wxdata = res.data.result;
if(res.data.result.pay_money == 0){
  wx.showToast({
    title: '订单已支付完成',
    icon:'success'
  })
  setTimeout(function(){
    wx.switchTab({
      url: '/pages/member/index/index',
    })
  },2000)
}
		});
	},
  back:function(){
    wx.switchTab({
      url: '/pages/index/index',
    })
  },
	pay: function () {
		var app = getApp();
    var that = this;
    search = setInterval(function(){
      that.order_search();
    },3000)
    setTimeout(function () {
      wx.navigateBack({
        delta: 2
      })
    }, 20000)
		var wxdata =       app.globalData.wxdata.wdata;
		var timeStamp = wxdata.timeStamp + "";
		var nonceStr = wxdata.nonceStr + "";
		var package1 = wxdata.package
		var sign = wxdata.sign;
			 wx.requestPayment({
			    
			    'nonceStr': nonceStr,
		       'package': package1,
			    'signType': 'MD5',
				'timeStamp': timeStamp,
			    'paySign': sign,
			    'success':function(res){
            that.order_search();
			    		console.log(res);
							wx.showToast({ title: '支付成功', icon: 'success', duration: 2000 })
                setTimeout(function doHandler(){
                  wx.redirectTo({
                    url: '/pages/member/index/index',
                  })
                },2000);
			    },
			    'fail':function(res){
			    		console.log(res);
            clearInterval(search)
							wx.showToast({ title: '支付失败', icon: 'none', duration: 2000 })
                setTimeout(function doHandler(){
                  wx.navigateBack({
                    delta: 2, // 回退前 delta(默认为1) 页面
                    success: function(res){
                      // success
                    },
                    fail: function() {
                      // fail
                    },
                    complete: function() {
                      // complete
                    }
                  })
                },2000);
			    }
			 })

			// update order
			/*var query = new AV.Query('Order');
			query.get(this.data.orderId).then(function (order) {
				order.set('status', 1);
				order.save();
				console.log('status: ' + 1);
			}, function (err) {
				
			});*/
	},

  order_search:function(e){
    var that = this;
    var i = 0;
    var orderid = that.data.order.order_id;
    server.postJSON("/Cart/orders",{order_id:orderid},function(e){
      i++;
      if(e.data.code == 1){
        clearInterval(search);
      }
      if(i >100){
        clearInterval(search);
      }
    })
  },
  onHide:function(){
    
  },
  onUnload:function(){
  }
})